/**
 * File Name   : MenuCfgService.java
 * Description : 
 */
package com.hh.admin.menu.service;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import com.hh.admin.menu.form.MenuCfgForm;
import com.hh.admin.menu.form.ParamMenuCfgBean;
import com.hh.admin.menu.form.ResultMenuCfgBean;
import com.hh.common.ibatis.dao.IQueryDao;
import com.hh.common.ibatis.dao.IUpdateDao;

/**
 * @author 冯昌硕
 * @create 2012-12-26 下午9:45:40
 */
public class MenuCfgService {
    
    private static final Logger log = Logger.getLogger(MenuCfgService.class);
    
    private IQueryDao queryDao;
    
    private IUpdateDao updateDao;

    /**
     * 分页查询菜单列表
     * @param form
     * @return
     * @throws SQLException
     * @create 2012-12-26 下午10:23:27
     */
    public List<ResultMenuCfgBean> getMenuList(MenuCfgForm form) throws SQLException {
        List<ResultMenuCfgBean> resultLst = null;
        
        int currentPage = form.getCurrentPage();
        int formCurrentPage = currentPage;
        if(formCurrentPage <= 0) {
            currentPage = 1;
        }
        
        int pageSize = form.getPageSize();
        int offset = (currentPage - 1) * pageSize;
        log.debug("菜单查询 : formCurrentPage=[" + formCurrentPage + "] currentPage=[" + currentPage + "] pageSize=[" + pageSize + "] offset=[" + offset + "]");
        
        ParamMenuCfgBean paramBean = new ParamMenuCfgBean();
        paramBean.setOffset(offset);
        paramBean.setPageSize(pageSize);
        paramBean.setMenuType(form.getMenuType());
        
        resultLst = queryDao.queryForObjectList("menu.selMenuList", paramBean);
        
        return resultLst;
    }
    
    /**
     * 统计菜单总数
     * @param form
     * @return
     * @create 2012-12-26 下午11:08:19
     */
    public int getMenuTotleCnt(MenuCfgForm form) {
        int totleCnt = 0;
        
        try {
            ParamMenuCfgBean paramBean = new ParamMenuCfgBean();
            paramBean.setMenuType(form.getMenuType());
            Integer rs = queryDao.queryForObject("menu.selMenuCount", paramBean, Integer.class);
            if(rs != null) {
                totleCnt = rs;
            }
        } catch (ClassCastException e) {
            log.error("menu.selMenuCount 查询ClassCastException异常：" + e.toString());
            e.printStackTrace();
        } catch (SQLException e) {
            log.error("menu.selMenuCount 查询SQLException异常：" + e.toString());
            e.printStackTrace();
        }
        
        return totleCnt;
    }
    
    /**
     * 删除菜单项记录
     * @param id 菜单ID
     * @return
     * @throws SQLException
     * @create 2012-12-26 下午11:55:52
     */
    public int deleteOneMenu(int id) throws SQLException {
        return updateDao.delete("menu.deleteMenu", id);
    }
    
    /**
     * 批量删除菜单项记录
     * @param ids 要删除的菜单项ID
     * @return
     * @throws SQLException
     * @create 2012-12-27 上午12:08:49
     */
    public int deleteManyMenu(Integer[] ids) throws SQLException {
        return updateDao.batchDelete("menu.deleteMenu", ids);
    }
    
    /**
     * 获取菜单记录详情
     * @param id 菜单ID
     * @return 菜单详情
     * @throws ClassCastException
     * @throws SQLException
     * @create 2012-12-27 下午7:49:09
     */
    public ResultMenuCfgBean getMenuDetail(int id) throws ClassCastException, SQLException {
        return queryDao.queryForObject("menu.selMenuDetail", id, ResultMenuCfgBean.class);
    }
    
    /**
     * 更新菜单信息
     * @param form 表单项
     * @return
     * @throws SQLException
     * @create 2012-12-27 下午8:20:26
     */
    public int updateMenu(MenuCfgForm form) throws SQLException {
        ParamMenuCfgBean paramBean = new ParamMenuCfgBean();
        paramBean.setId(form.getId());
        paramBean.setMenuType(form.getMenuType());
        paramBean.setDisplayName(form.getDisplayName());
        paramBean.setDisplayOrder(form.getDisplayOrder());
        paramBean.setUrl(form.getUrl());
        paramBean.setStatus(form.getStatus());
        
        log.debug("更新菜单信息 paramBean=[" + paramBean + "]");
        return updateDao.update("menu.updateMenu", paramBean);
    }
    
    /**
     * 添加新菜单记录
     * @param form
     * @return
     * @throws SQLException
     * @create 2012-12-27 下午9:09:40
     */
    public int insertMenu(MenuCfgForm form) throws SQLException {
        ParamMenuCfgBean paramBean = new ParamMenuCfgBean();
        paramBean.setMenuType(form.getMenuType());
        paramBean.setDisplayName(form.getDisplayName());
        paramBean.setDisplayOrder(form.getDisplayOrder());
        paramBean.setUrl(form.getUrl());
        paramBean.setStatus(form.getStatus());
        paramBean.setCreateUser(form.getCreateUser());
        
        log.debug("新增菜单信息 paramBean=[" + paramBean + "]");
        return (Integer) updateDao.insert("menu.insertMenu", paramBean);
    }

    public void setQueryDao(IQueryDao queryDao) {
        this.queryDao = queryDao;
    }

    public void setUpdateDao(IUpdateDao updateDao) {
        this.updateDao = updateDao;
    }
    
}
